package com.lanyun.serviceimpl;

import com.lanyun.dao.SalesOrderDao;
import com.lanyun.pojo.Admin;
import com.lanyun.pojo.Customer;
import com.lanyun.pojo.SalesOrder;
import com.lanyun.service.SalesOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class SalesOrderServiceImpl implements SalesOrderService {
    @Autowired
    private SalesOrderDao salesOrderDao;

    @Override
    public List<SalesOrder> getAll() {
        return salesOrderDao.getAll();
    }

    @Override
    public int addOrderAndDetail(String cid,String salesmanId,String[] pids, String[] amounts) {
        //录入日期
        Date enterDate = new Date();
        //销售员
        Admin salesman = new Admin();
        salesman.setId(Integer.parseInt(salesmanId));
        //客户
        Customer customer = new Customer();
        customer.setId(Integer.parseInt(cid));

        SalesOrder salesOrder = new SalesOrder(null,enterDate,null,salesman,null,customer,"未出库",1);
        int result = salesOrderDao.addSalesOrder(salesOrder);
        //System.out.println("成功插入"+result+"条记录！");
        //System.out.println("获取自增的id为："+salesOrder.getId());
        List<Map<String,String>> datas = new ArrayList<>();
        for (int i = 0;i<pids.length;i++){
            Map<String,String> map = new HashMap<>();
            map.put("pid",pids[i]);
            map.put("amount",amounts[i]);
            datas.add(map);
        }

        //int a = 5/0;//测试事务是否生效
        int num = salesOrderDao.addSalesOrderDetail(String.valueOf(salesOrder.getId()), datas, "未出库");
        if(result!=0&&num!=0){
            return 1;
        }else{
            return 0;
        }
    }

    @Override
    public SalesOrder getById(String id) {
        return salesOrderDao.getById(id);
    }

    @Override
    public List<Map<String, String>> getDetailsById(String id) {
        return salesOrderDao.getDetailsById(id);
    }

    @Override
    public int updateDetail(String oid, String[] pids, String[] amounts) {
        int num1 = salesOrderDao.delSalesOrderDetail(oid);
        List<Map<String,String>> datas = new ArrayList<>();
        for (int i = 0;i<pids.length;i++){
            Map<String,String> map = new HashMap<>();
            map.put("pid",pids[i]);
            map.put("amount",amounts[i]);
            datas.add(map);
        }
        int num2 = salesOrderDao.addSalesOrderDetail(oid,datas,"未出库");
        if(num1!=0&&num2!=0){
            return 1;
        }else{
            return 0;
        }
    }

    @Override
    public List<Map<String, String>> getProductsByKeeperAndOrder(String oid, String keeperId) {
        return salesOrderDao.getProductsByKeeperAndOrder(oid,keeperId);
    }

    @Override
    public int outWareHouse(String oid, String pid, String wid, String amount, String keeperId) {
        //减库存
        int num1 = salesOrderDao.changeStock(pid,wid,Integer.parseInt(amount));
        //System.out.println("num1:"+num1);
        //修改中间表的商品状态
        int num2 = salesOrderDao.changeOrderProductState(oid,pid,"已出库");
        //System.out.println("num2:"+num2);
        //操作日期
        Date operateDate = new Date();
        //将库存信息记录在库存表中
        int num3 = salesOrderDao.addStockInfo(keeperId,operateDate,wid,pid,amount,"出库");
        //System.out.println("num3:"+num3);
        //通过该销售订单是否有未出库的商品，判断该订单是否已出库
        int count = salesOrderDao.getProductsCountByState(oid,"未出库");
        //System.out.println("未出库的商品有"+count+"个");
        //如果没有未出库的商品，就修改订单的状态为已出库
        if (count==0){
            int num4 = salesOrderDao.outTheOrder(oid, operateDate, keeperId, "已出库");
            //System.out.println("num4:"+num4);
            return num1*num2*num3*num4>0?2:0;
        }
        return num1*num2*num3>0?1:0;
    }

    @Override
    public List<SalesOrder> getByCondition(String customerName, String salesmanName, String keeperName, Date enterDateStart, Date enterDateEnd, Date outDateStart, Date outDateEnd, String state) {
        return salesOrderDao.getByCondition(customerName,salesmanName,keeperName,enterDateStart,enterDateEnd,outDateStart,outDateEnd,state);
    }

    @Override
    public int isBegined(String oid) {
        return salesOrderDao.getProductsCountByState(oid,"已出库");
    }

    @Override
    public int delById(String oid) {
        return salesOrderDao.delById(oid);
    }

    @Override
    public int delBatch(String[] ids) {
        return salesOrderDao.delBatch(ids);
    }

    @Override
    public List<SalesOrder> getByKeeper(Admin keeper) {
        return salesOrderDao.getByKeeper(String.valueOf(keeper.getId()));
    }
}
